<template>
  <div>
    <div>
      <h1>品牌列表</h1>
    </div>
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="请选择关键字/品牌名称"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="loadBrandList()">查询</el-button>
          <el-button type="success" @click="onaddBrand()">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table :data="prodList" border style="width: 100%">
        <el-table-column prop="brandId" label="品牌Id" />
        <el-table-column prop="brandName" label="品牌spu" />
        <el-table-column prop="brandUse" label="使用数量" />
        <el-table-column prop="brandTop" label="排序" />
        <el-table-column prop="brandStatus" label="状态">
          <template #default="scope">
            <el-switch
              v-model="scope.row.brandStatus"
              :active-value="0"
              :inactive-value="1"
              active-text="开启"
              inactive-text="隐藏"
              @change="updateStatus(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createBy" label="创建人" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作" />
      </el-table>
    </div>
    <div>
      <el-pagination
        v-model:current-page="searchForm.pageNum"
        v-model:page-size="searchForm.pageSize"
        :page-sizes="[2, 3, 4, 5]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加品牌" width="500">
        <el-form ref="ruleFormRef" :model="form" :rules="rules">
          <el-form-item
            label="品牌名称"
            prop="brandName"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.brandName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="排序"
            :label-width="formLabelWidth"
            prop="brandTop"
          >
            <el-input-number v-model="form.brandTop" :min="1" />
          </el-form-item>
          <el-form-item
            label="品牌状态"
            :label-width="formLabelWidth"
            prop="brandStatus"
          >
            <el-radio-group v-model="form.brandStatus">
              <el-radio :value="0" size="large">开启</el-radio>
              <el-radio :value="1" size="large">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="onSubmitAdd(ruleFormRef)">
              添加
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getBrandList, addBrand, updateBrand } from "../api/api";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
const ruleFormRef = ref<FormInstance>();
const prodList = ref([]);
const total = ref(0);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
});
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const form = ref({
  brandName: "",
  brandStatus: 0,
  brandTop: 0,
});
//校验
const rules = ref<FormRules>({
  brandName: [{ required: true, message: "品牌名称不能为空", trigger: "blur" }],
  brandStatus: [
    { required: true, message: "品牌状态不能为空", trigger: "blur" },
  ],
  brandTop: [{ required: true, message: "排序不能为空", trigger: "blur" }],
});
//分页
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadBrandList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadBrandList();
};
//获取品牌列表
const loadBrandList = () => {
  getBrandList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      prodList.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
//修改状态
const updateStatus = (row: any) => {
  updateBrand(row).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      loadBrandList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
onMounted(() => {
  loadBrandList();
});
//添加品牌
const onaddBrand = () => {
  dialogFormVisible.value = true;
};
const onSubmitAdd = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      addBrand(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          loadBrandList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>

<style scoped></style>
